/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Hmfs-tools log
 */

#ifndef HMFS_LOG_H
#define HMFS_LOG_H

#include "constant.h"
#include "hmfs_command.h"
#include "hmfs_utils.h"
#ifdef PRINT_TO_HILOG
#include "hilog/log.h"
#endif

namespace OHOS {
namespace Hmfs {
#ifndef PRINT_TO_HILOG
#define HMFS_INFO(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 0) { \
            printf("Info: " fmt "\n", ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_ERROR(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 0) { \
            printf("Error: " fmt "\n", ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_DEBUG(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 1) { \
            printf("[%s:%d]Debug: " fmt "\n", (__FILE__), (__LINE__), ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_MINOR_DEBUG(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 2) { \
            printf("[%s:%d]Debug: " fmt "\n", (__FILE__), (__LINE__), ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_TRIVIAL_DEBUG(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 3) { \
            printf("[%s:%d]Debug: " fmt "\n", (__FILE__), (__LINE__), ##__VA_ARGS__); \
        } \
    } while (0)

#else

#ifndef HMFS_FILE_NAME
#define HMFS_FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
#endif

#define HMFS_DOMAIN (0xD002C00 + 0x12)
#ifndef HMFS_LABEL
#define HMFS_LABEL "HMFS_TOOLS"
#endif

#undef LOG_TAG
#define LOG_TAG HMFS_LABEL
#undef LOG_DOMAIN
#define LOG_DOMAIN HMFS_DOMAIN

#define HMFS_INFO(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 0) { \
            HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (HMFS_FILE_NAME), (__LINE__), ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_ERROR(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 0) { \
            HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (HMFS_FILE_NAME), (__LINE__), ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_DEBUG(fmt, ...) \
    do { \
        if (CmdParser::GetDebugLevel() >= 1) { \
            HILOG_DEBUG(LOG_CORE, "[%{public}s:%{public}d]" fmt, (HMFS_FILE_NAME), (__LINE__), ##__VA_ARGS__); \
        } \
    } while (0)

#define HMFS_MINOR_DEBUG(level, debugLevel, fmt, ...) \
    HMFS_DEBUG(level, debugLevel, fmt, ##__VA_ARGS__)
#define HMFS_TRIVIAL_DEBUG(level, debugLevel, fmt, ...) \
    HMFS_DEBUG(level, debugLevel, fmt, ##__VA_ARGS__)
#endif

/*
 * Print to console
 */
template <typename T>
void PrintToConsole(const char *memberName, const T &member)
{
    static_assert(std::is_integral<T>::value, "Type must be integral");
    T value = GetLeValue(member);
    printf("%-35s ", memberName);
    if constexpr (sizeof(T) == sizeof(uint64_t)) {
        printf("\t[0x%16" PRIx64 " : %" PRIu64 "]\n", value, value);
    } else if constexpr (sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint8_t)) {
        printf("\t[0x%16x : %u]\n", value, value);
    } else {
        printf("\t[0x%16x : %u]\n", value, value);
    }
}

#define PRINT_TO_COLSOLE(ptr, member) PrintToConsole(#member, (ptr)->member)

/*-------------------------------- check & assert --------------------------------*/
#define HMFS_ASSERT(exper) \
    do { \
        if (!(exper)) { \
            HMFS_ERROR("[HMFS_ASSERT %s, %s:%d]", #exper, __FILE__, __LINE__); \
            exit(-1); \
        } \
    } while (0)

#define HMFS_ASSERT_FREE(exper, buf) \
    do { \
        if (!(exper)) { \
            HMFS_ERROR("[HMFS_ASSERT %s, %s:%d]", #exper, __FILE__, __LINE__); \
            free(buf); \
            exit(-1); \
        } \
    } while (0)

#define HMFS_ASSERT_FREE_PTRS(exper, buf1, buf2) \
    do { \
        if (!(exper)) { \
            HMFS_ERROR("[HMFS_ASSERT %s, %s:%d]", #exper, __FILE__, __LINE__); \
            free(buf1); \
            free(buf2); \
            exit(-1); \
        } \
    } while (0)

#define HMFS_CHECK(exper, retCode, fmt, ...) \
    if (!(exper)) { \
        HMFS_ERROR(fmt, ##__VA_ARGS__); \
        retCode; \
    }

#define HMFS_CHECK_ONLY_EXPER(exper, retCode) \
    if (!(exper)) { \
        retCode; \
    }

#define HMFS_CHECK_ONLY_LOG(exper, fmt, ...) \
    if (!(exper)) { \
        HMFS_ERROR(fmt, ##__VA_ARGS__); \
    }

} // namespace Hmfs
} // namespace OHOS
#endif // HMFS_LOG_H
